package com.easyJavaDemo.controller;

import com.easyJavaDemo.service.GoodsService;
import com.easyJavaDemo.common.result.Result;
import com.easyJavaDemo.entity.vo.PaginationResultVO;
import com.easyJavaDemo.entity.po.Goods;
import com.easyJavaDemo.entity.query.GoodsQuery;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
 * 商品表Controller接口
 */
@RestController
@RequestMapping("/goods")
public class GoodsController{

	/**
	 * GoodsService对象
	 */
	@Autowired
	private GoodsService goodsService;


	/**
	 * 新增
	 */
	@PostMapping
	public Result add(@RequestBody Goods goods){
		goodsService.add(goods);
		return Result.success();
	}

	/**
	 * 新增或修改
	 */
	@PutMapping
	public Result addOrUpdate(@RequestBody Goods goods){
		goodsService.addOrUpdate(goods);
		return Result.success();
	}

	/**
	 * 批量新增
	 */
	@PostMapping("/list")
	public Result addBatch(@RequestBody List<Goods> goodsList){
		goodsService.addBatch(goodsList);
		return Result.success();
	}

	/**
	 * 批量新增或修改
	 */
	@PutMapping("/list")
	public Result addOrUpdateBatch(@RequestBody List<Goods> goodsList){
		goodsService.addOrUpdateBatch(goodsList);
		return Result.success();
	}

	/**
	 * 查询
	 */
	@GetMapping
	public Result<List<Goods>> getList(GoodsQuery goodsQuery){
		List<Goods> list = goodsService.getList(goodsQuery);
		return Result.success(list);
	}

	/**
	 * 分页查询
	 */
	@GetMapping("/page")
	public Result<PaginationResultVO<Goods>> page(GoodsQuery goodsQuery){
		PaginationResultVO<Goods> result = goodsService.getPageList(goodsQuery);
		return Result.success(result);
	}


	/**
	 * 根据Id查询
	 */
	@GetMapping("/id")
	public Result<Goods> getById(@RequestBody Goods goods){
		Integer id = goods.getId();
		Goods res = goodsService.getById(id);
		return Result.success(res);
	}

	/**
	 * 根据Id修改
	 */
	@PutMapping("/id")
	public Result updateById(@RequestBody Goods goods){
		Integer id = goods.getId();
		goodsService.updateById(goods, id);
		return Result.success();
	}

	/**
	 * 根据Id删除
	 */
	@DeleteMapping("/id")
	public Result deleteById(@RequestBody Goods goods){
		Integer id = goods.getId();
		goodsService.deleteById(id);
		return Result.success();
	}

	/**
	 * 根据NameAndPrice查询
	 */
	@GetMapping("/nameAndPrice")
	public Result<Goods> getByNameAndPrice(@RequestBody Goods goods){
		String name = goods.getName();
		Integer price = goods.getPrice();
		Goods res = goodsService.getByNameAndPrice(name, price);
		return Result.success(res);
	}

	/**
	 * 根据NameAndPrice修改
	 */
	@PutMapping("/nameAndPrice")
	public Result updateByNameAndPrice(@RequestBody Goods goods){
		String name = goods.getName();
		Integer price = goods.getPrice();
		goodsService.updateByNameAndPrice(goods, name, price);
		return Result.success();
	}

	/**
	 * 根据NameAndPrice删除
	 */
	@DeleteMapping("/nameAndPrice")
	public Result deleteByNameAndPrice(@RequestBody Goods goods){
		String name = goods.getName();
		Integer price = goods.getPrice();
		goodsService.deleteByNameAndPrice(name, price);
		return Result.success();
	}

}
